*** Outpatient substitutes 

*** First, make a panel of outpatient spending by provider 

cd /disk/aging/medicare/data/harm/100pct/op/
* Loop over years of files; grab provider numbers and payments; panelize by month
foreach YEAR of numlist 2002/2016{
	use provider prvdrpmt thru_dt using `YEAR'/opc`YEAR'.dta
	gen dt = mofd(thru_dt)
	collapse(sum) prvdrpmt, by(provider dt)
	save /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/`YEAR'_panel.dta
}


************************************************
**** Clean into 1 panel
**** Tag defendant providers

cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/
use 2002_panel.dta, clear 
foreach YEAR of numlist 2003/2016{
	append using `YEAR'_panel.dta
}

rename provider prvdrnum

replace prvdrnum = "StJo" if prvdrnum == "110082"
replace prvdrnum = "Wheaton" if prvdrnum =="241304"
replace prvdrnum = "ElCentro" if prvdrnum == "050045"
replace prvdrnum = "Overlook" if prvdrnum == "310051"
replace prvdrnum = "MortonPlant" if inlist(prvdrnum, "100127", "100075", "100063", "100067", "100265", "100043")
replace prvdrnum = "Shands" if inlist(prvdrnum, "100001", "100113", "100082", "100102", "101310", "101301")

* ** Tag CHS hospitals by a list of CCNs stored in a file
merge m:1 prvdrnum using chsccn.dta
replace prvdrnum = "chs" if chs == 1
drop _merge chs


* *** Recollapse so 1 series per provider
rename prvdrpmt pmt_amt 
collapse (sum) pmt_amt, by(prvdrnum dt)
save op_providerpanel_dft.dta, replace 

* *********************************************************
* *** Defendant graphs

local StJoTreat = mofd(mdy(11,16,2004))
local StJoSettle = mofd(mdy(12,21,2007))
twoway scatter pmt_amt dt if prvdrnum == "StJo", ylab(#3) xline(`StJoTreat') xline(`StJoSettle')

* local WheatonTreat = mofd(mdy(10,19,2004))
* local WheatonSettle = mofd(mdy(1,7,2010))
* twoway scatter pmt_amt dt if prvdrnum == "Wheaton", ylab(#3) xline(`WheatonTreat') xline(`WheatonSettle') 

* local ElCentroTreat = mofd(mdy(3,20,2006))
* local ElCentroSettle = mofd(mdy(9,20,2010))
* twoway scatter pmt_amt dt if prvdrnum == "ElCentro", ylab(#3) xline(`ElCentroTreat') xline(`ElCentroSettle') 

* local OverlookTreat = mofd(mdy(4,25,2008))
* local OverlookSettle = mofd(mdy(6,21,2012))
* twoway scatter pmt_amt dt if prvdrnum == "Overlook", ylab(#3) xline(`OverlookTreat') xline(`OverlookSettle') 

local MortonPlantTreat = mofd(mdy(8,27,2008)) 
local MortonPlantSettle = mofd(mdy(11,20,2012))
twoway scatter pmt_amt dt if prvdrnum == "MortonPlant", ylab(#3) xline(`MortonPlantTreat') xline(`MortonPlantSettle')

* local ShandsTreat = mofd(mdy(8,30,2008)) 
* local ShandsSettle = mofd(mdy(8,8,2013))
* twoway scatter pmt_amt dt if prvdrnum == "Shands", ylab(#3) xline(`ShandsTreat') xline(`ShandsSettle')

local chsTreat = mofd(mdy(1,7,2009))
local chsSettle= mofd(mdy(8,4,2014))
twoway scatter pmt_amt dt if prvdrnum == "chs", ylab(#3) xline(`chsTreat') xline(`chsSettle')

***************************************************************************
*****************************************************************

** Controls: all hospitals in non-treated states, without missing data
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes
use op_providerpanel_dft.dta, clear
drop if dt > mofd(mdy(12,31,2016))

** Filter by state 
gen provider_state = substr(prvdrnum, 1, 2)
gen control_state = 1 if  provider_state == "06" | provider_state == "07"|provider_state == "09" | provider_state == "12" | provider_state == "13"|provider_state == "16" | provider_state == "17" | provider_state == "20"|provider_state == "21"| provider_state == "22" | provider_state == "23"|provider_state == "27"
replace control_state = 1 if provider_state == "28" | provider_state == "29"|provider_state == "30"| provider_state == "33" | provider_state == "35"|provider_state == "36" | provider_state == "38" | provider_state == "41"|provider_state == "43" | provider_state == "47" | provider_state == "52"
replace control_state = 0 if mi(control_state)
keep if control_state == 1| inlist(prvdrnum, "StJo", "Wheaton", "ElCentro", "Overlook", "MortonPlant", "Shands", "chs")
drop provider_state
drop control_state


* Encode into temp groups to fill in panel 
encode prvdrnum, gen(g)
label var pmt_amt "Total Outpatient Payments"
label var dt "Month"
format dt %tm

tsset g dt 
tsfill, full
replace pmt_amt = 0 if mi(pmt_amt)
tsset g dt

** Tag controls by prvdrnum
** First need to fill in prvdrnum for empty panel

** Sort groups by time, carryforward provider number
sort g dt 
by g: carryforward prvdrnum, replace

** Sort groups by reverse time, carryforward provider number
count if mi(prvdrnum)
gen temp = 1000 - dt 
sort g temp 
by g: carryforward prvdrnum, replace 
count if mi(prvdrnum)
assert (r(N) == 0)
drop temp 

*** Tag controls by prvdrnum
gen control = 0 if inlist(prvdrnum, "StJo", "Wheaton", "ElCentro", "Overlook", "MortonPlant", "Shands", "chs")
replace control = 1 if!inlist(prvdrnum, "StJo", "Wheaton", "ElCentro", "Overlook", "MortonPlant", "Shands", "chs") 
count if mi(control)
assert(r(N) == 0)


*** Drop controls with missing months
bysort g: egen minmonth = min(pmt_amt)
drop if control == 1 & minmonth <= 0 
drop g 


save /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/op_ipop_controls_panel.dta, replace


*********************************************************
*** Need graph of full outpatient series to show no global effects
** 

cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes
use op_providerpanel_dft.dta 
collapse (sum) pmt_amt, by(dt)

local WheatonTreat = mofd(mdy(10,19,2004))
drop if mi(dt)
drop if dt > mofd(mdy(12,31,2016))
drop if dt < mofd(mdy(1,1,2002))


format dt %tm_Mon_YY
twoway scatter pmt_amt dt, xline(`WheatonTreat') xlab(516 `WheatonTreat' 564 588 612 636 660) xtitle("Month") ytitle("Outpatient $/Month") graphregion(color(white)) ylab(0 "0" 2000000000 "$2 Bil" 4000000000 "$4 Bil" 6000000000 "$6 Bil" ) yscale(range(0 650000000))
graph export /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/graphs/AllOP.pdf



